package com.firesrc.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.firesrc.service.KeywordService;
import com.firesrc.service.impl.KeywordServiceImpl;

/**
 * Servlet implementation class TopicServlet
 */
public class TopicServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TopicServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		Map<String, String> map = new HashMap<String, String>();
		KeywordService service = new KeywordServiceImpl();
		List<String> topicList = service.getTopicList();
		for (int i = 0; i < topicList.size(); i++) {
			map.put(String.valueOf(i), topicList.get(i));
		}
		String type = request.getParameter("type");
		if ("set".equals(type)) {
			String name = request.getParameter("name");
			String topic = map.get(name);
			service.setMailSendByTopic(topic);
			System.out.println("TopicServlet.doPost()" + name);
		}

		request.setAttribute("map", map);
		request.getRequestDispatcher("/topiclist.jsp").forward(request, response);
	}

}
